﻿using System;
using System.Collections.Generic;

namespace DuckBehaviorWithInterface
{
    public interface IFlyBehavior
    {
        void Fly();
    }

    public interface IQuackBehavior
    {
        void Quack();
    }

    public class FlyWithWings : IFlyBehavior
    {
        public void Fly()
        {
            Console.WriteLine("我用翅膀飞行！");
        }
    }

    public class FlyNoWay : IFlyBehavior
    {
        public void Fly()
        {
            Console.WriteLine("我不能飞行！");
        }
    }

    public class FlyRocketPowered : IFlyBehavior
    {
        public void Fly()
        {
            Console.WriteLine("我用火箭动力飞行！");
        }
    }


    public class QuackBehavior : IQuackBehavior
    {
        public void Quack()
        {
            Console.WriteLine("嘎嘎叫！");
        }
    }

    public class Squeak : IQuackBehavior
    {
        public void Quack()
        {
            Console.WriteLine("吱吱叫！");
        }
    }

    public class MuteQuack : IQuackBehavior
    {
        public void Quack()
        {
            Console.WriteLine("<沉默>");
        }
    }

    public abstract class Duck
    {
        protected IFlyBehavior flyBehavior;
        protected IQuackBehavior quackBehavior;

        public Duck() { }

        public abstract void Display();

        public void PerformFly()
        {
            flyBehavior.Fly();
        }

        public void PerformQuack()
        {
            quackBehavior.Quack();
        }

        public void Swim()
        {
            Console.WriteLine("所有鸭子都会游泳，甚至包括诱饵鸭！");
        }

        public void SetFlyBehavior(IFlyBehavior fb)
        {
            flyBehavior = fb;
        }

        public void SetQuackBehavior(IQuackBehavior qb)
        {
            quackBehavior = qb;
        }
    }

    public class MallardDuck : Duck
    {
        public MallardDuck()
        {
            flyBehavior = new FlyWithWings();
            quackBehavior = new QuackBehavior();
        }

        public override void Display()
        {
            Console.WriteLine("我是一只绿头鸭！");
        }
    }

    public class RubberDuck : Duck
    {
        public RubberDuck()
        {
            flyBehavior = new FlyNoWay();
            quackBehavior = new Squeak();
        }

        public override void Display()
        {
            Console.WriteLine("我是一只橡皮鸭！");
        }
    }

    public class DecoyDuck : Duck
    {
        public DecoyDuck()
        {
            flyBehavior = new FlyNoWay();
            quackBehavior = new MuteQuack();
        }

        public override void Display()
        {
            Console.WriteLine("我是一只诱饵鸭！");
        }
    }

    public class ModelDuck : Duck
    {
        public ModelDuck()
        {
            flyBehavior = new FlyNoWay();
            quackBehavior = new QuackBehavior();
        }

        public override void Display()
        {
            Console.WriteLine("我是一只模型鸭！");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== 鸭子行为模拟程序 ===\n");

            Duck mallard = new MallardDuck();
            Duck rubber = new RubberDuck();
            Duck decoy = new DecoyDuck();
            Duck model = new ModelDuck();

            TestDuck(mallard, "绿头鸭");
            TestDuck(rubber, "橡皮鸭");
            TestDuck(decoy, "诱饵鸭");

            Console.WriteLine("\n=== 动态改变模型鸭的行为 ===");
            model.Display();
            model.PerformFly();
            model.PerformQuack();

            Console.WriteLine("\n给模型鸭装上火箭动力！");
            model.SetFlyBehavior(new FlyRocketPowered());
            model.PerformFly();

        }

        static void TestDuck(Duck duck, string duckType)
        {
            Console.WriteLine($"\n--- 测试{duckType} ---");
            duck.Display();
            duck.PerformFly();
            duck.PerformQuack();
            duck.Swim();
        }
    }
}